package com.liang.paul.officelite.fragments;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.liang.paul.officelite.R;
import com.liang.paul.officelite.adapters.FileAdpter;
import com.liang.paul.officelite.beams.FileBeam;
import com.liang.paul.officelite.utils.FileUtils;
import com.shamanland.fab.FloatingActionButton;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class FileFragment extends Fragment {

    View rootview;
    FloatingActionButton btn_add;
    ListView filelist;
    ArrayList<FileBeam> datas;
    FileAdpter adpter;
    String teamId;


    public FileFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fragment_file, container, false);
        teamId = getArguments().getString("teamId");
        initView();
        return rootview;
    }

    private void initView(){
        btn_add = (FloatingActionButton) rootview.findViewById(R.id.btn_file_addfile);
        filelist = (ListView) rootview.findViewById(R.id.list_file_list);
        datas = new ArrayList<FileBeam>();
        adpter = new FileAdpter(datas, getActivity());
        filelist.setAdapter(adpter);
        getData();
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 110);
            }
        });
    }

    private void getData(){
        BmobQuery<FileBeam> query = new BmobQuery<FileBeam>();
        query.addWhereEqualTo("teamId", teamId);
        query.setLimit(25);
        query.order("-createdAt");
        query.findObjects(getActivity(), new FindListener<FileBeam>() {
            @Override
            public void onSuccess(List<FileBeam> list) {
                for (FileBeam beam : list) {
                    datas.add(beam);
                    adpter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 110){
            String path = FileUtils.getPath(getActivity(), data.getData());
            final ProgressDialog progressDialog = new ProgressDialog(getActivity());
            //Toast.makeText(getActivity(), path, Toast.LENGTH_SHORT).show();
            final BmobFile file = new BmobFile(new File(path));
            file.upload(getActivity(), new UploadFileListener() {

                @Override
                public void onStart() {
                    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    progressDialog.setTitle("正在上传...");
                    progressDialog.setCancelable(false);
                    progressDialog.show();
                    progressDialog.setProgress(1);
                }

                @Override
                public void onProgress(Integer value) {
                    progressDialog.setProgress(value);
                }

                @Override
                public void onSuccess() {
                    progressDialog.cancel();
                    upLoad(file);
                }

                @Override
                public void onFailure(int i, String s) {
                    progressDialog.cancel();
                    Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
                }
            });
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void upLoad(BmobFile file){
        FileBeam beam = new FileBeam();
        beam.setTeamId(teamId);
        beam.setFile(file);
        beam.save(getActivity(), new SaveListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(getActivity(), "上传成功", Toast.LENGTH_SHORT).show();
                datas.clear();
                adpter.notifyDataSetChanged();
                getData();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
